package formal.queueStack;

import java.util.Stack;

/**
 * @author DengYuan2
 * @create 2021-01-28 20:30
 */
public class E_155 {

    /**
     * 大神
     */
    class MinStack {

        private Stack<Integer> dataStack;
        private Stack<Integer> minStack;
        private int min = Integer.MAX_VALUE;

        public MinStack() {
            dataStack = new Stack<>();
            minStack = new Stack<>();
        }

        public void push(int x){
            dataStack.push(x);
            min=Math.min(x,min);
            minStack.push(min);
        }

        public void pop(int x){
            dataStack.pop();
            minStack.pop();
            min=minStack.isEmpty()?Integer.MAX_VALUE:minStack.peek();
        }

        public int top(){
            return dataStack.peek();
        }

        public int getMin(){
            return minStack.peek();
        }



    }

    class MinStack2{
        private Stack<int[]> stack;

        public MinStack2(){
            stack=new Stack<>();
        }

        //栈里存放数组，存放x和x放入时栈中最小的值
        public void push(int x){
            if (stack.isEmpty()){
                stack.push(new int[]{x,x});
            }else {
                stack.peek();
                stack.push(new int[]{x,Math.min(x,stack.peek()[1])});
            }
        }

        public void pop(){
            stack.pop();
        }

        public int top(){
            return stack.peek()[0];
        }

        public int getMin(){
            return stack.peek()[1];
        }
    }
}
